//
//  RecommendViewController.m
//  Baisibudejie
//
//  Created by yu hu on 2017/3/21.
//  Copyright © 2017年 yu hu. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendCategoryTableView.h"
#import "CategoryTableViewCell.h"
#import "RecommendCategory.h"
#import "RecommendUserTableView.h"
#import "RecommendUserTableViewCell.h"
#import "RecommendUser.h"
@interface RecommendViewController ()<UITableViewDelegate , UITableViewDataSource>
/** 左侧分类表 */
@property (weak, nonatomic) IBOutlet RecommendCategoryTableView *categoryTableView;
/** 右侧用户数据表 */
@property (weak, nonatomic) IBOutlet RecommendUserTableView *userTableView;

/** 分类数据 */
@property (strong,nonatomic)NSArray * categories;
/** 用户数据 */
@property (strong,nonatomic)NSArray * users;
@end

@implementation RecommendViewController

static NSString *categoryindentifer = @"category";
static NSString *userindentifer = @"recommendUser";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTableView];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"a"] = @"category";
    params[@"c"] = @"subscribe";
    
    // 设置请求超时
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    config.timeoutIntervalForRequest = 7.0;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
    

    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%f",1.0* downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
            self.categories = [RecommendCategory mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            
            // 刷新分类数据表格
            [self.categoryTableView reloadData];
            
            [self.categoryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
            
            [SVProgressHUD dismiss];
            
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(error.code == -1001)
        {
            [SVProgressHUD showErrorWithStatus:@"请求超时啦"];
            

        }else
        {
        [SVProgressHUD showErrorWithStatus:@"加载失败!"];
        }
    }];
    
}

- (void)setUpTableView
{
    self.navigationItem.title = @"推荐关注";
    self.view.backgroundColor = XMGGlobalBg;
    //隐藏多余cell
    self.categoryTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.userTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    // 左边分类表注册cell
    [self.categoryTableView registerNib:[UINib nibWithNibName:NSStringFromClass([CategoryTableViewCell class]) bundle:nil] forCellReuseIdentifier:categoryindentifer];
    
    // 右边用户表注册cell
    [self.userTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RecommendUserTableViewCell class]) bundle:nil] forCellReuseIdentifier:userindentifer];
    // 设置inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.userTableView.contentInset = self.categoryTableView.contentInset;
    self.userTableView.rowHeight = 70;

}

#pragma mark --- 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.categoryTableView) {
        return self.categories.count;
    }else
    {
        return self.users.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.categoryTableView) {
        CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryindentifer];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.recommendCategory = self.categories[indexPath.row];
        return cell;
    }else{
    
        RecommendUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:userindentifer];
        cell.recommendUser = self.users[indexPath.row];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendCategory *recommendCategory = self.categories[indexPath.row];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"subscribe";
    params[@"category_id"] = @(recommendCategory.id);
    
    
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.users = [RecommendUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        // 刷新后面的tableview
        [self.userTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        XMGLog(@"数据加载失败!");
    }];
}


@end
